/*
*Container.fx
*
*Created on Aug 7, 2011, 19:07:58 PM
*/

package derbyclnt;

import derbyclnt.Config;
import java.lang.Math;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import java.util.Random;

public class Container extends CustomNode {
    
    public var turnLeft:Boolean = false;
    public var turnRight:Boolean = false;
    
    public var accelerate:Boolean = false;
    public var applyBreak:Boolean = false;
    
    public var car: Car = Car {
        posX: Config.SCREEN_WIDTH/2
        posY: Config.SCREEN_HEIGHT/2    
    }
    
    public var gameBoard: Group = Group {
        content: [
        	car
        ]
        
        focusTraversable: true;
        onKeyPressed: function(e:KeyEvent):Void {
            if (e.code == KeyCode.VK_ENTER) {
                if (not gameLoop.running or gameLoop.paused) {
                    startGame();
                }
            }
            if (e.code == KeyCode.VK_LEFT) {
                turnLeft = true;
            }
            if (e.code == KeyCode.VK_RIGHT) {
                turnRight = true;
            }
            if(e.code == KeyCode.VK_UP){
                car.moveAngle = car.faceAngle;
                accelerate = true;
            }
            if(e.code == KeyCode.VK_DOWN){
                car.moveAngle = car.faceAngle;
                applyBreak = true;
            }
        }
        onKeyReleased: function(e:KeyEvent):Void {
            if (e.code == KeyCode.VK_LEFT) {
                turnLeft = false;
            }
            if (e.code == KeyCode.VK_RIGHT) {
                turnRight = false;
            }
            if(e.code == KeyCode.VK_UP){
                accelerate = false;
            }
            if(e.code == KeyCode.VK_DOWN){
                applyBreak = false;
            }
        }
    }
    
    
    
    override public function create(): Node {
        return gameBoard;
    }
    
    def gameLoop:Timeline = Timeline {
        repeatCount : Timeline.INDEFINITE
        keyFrames: [
        	KeyFrame {
        	    time: Config.REFRESH_RATE
        	    action: function() {
        	        updateGame();
        	    }
        	}
        ]
    }
    
    public function startGame(): Void {
        gameLoop.play();
    }
    
    public function updateGame():Void {
        updateCar();
    }
    
    public function updateCar():Void {
        if (turnLeft) {
            car.faceAngle -= Config.CAR_ROTATION_VELOCITY;
        }
        if (turnRight) {
            car.faceAngle += Config.CAR_ROTATION_VELOCITY;
        }
        
        if(accelerate and car.velocity < 10){
            car.moveAngle = car.faceAngle;
            car.velocity += 1;
        }else{
            if(car.velocity > 0){
                car.velocity -= 0.05;
            }
        }
        if(applyBreak and car.velocity > -3){
            car.velocity -=1;
        }
        var speedX = Math.sin(Math.toRadians(car.faceAngle)) * car.velocity;
        var speedY = -Math .cos(Math.toRadians(car.faceAngle)) * car.velocity;
        var newX = speedX + car.posX;
        var newY = speedY + car.posY;
        car.posX = newX;
        car.posY = newY;

        //handle wrapping for when ship goes out of bounds...
        if(car.posX < 0 - car.width/2 ) {
            //ship has moved off the left side,
            //so make it wrap to the right side...
            car.posX = Config.SCREEN_WIDTH;
        }
        if(car.posX > Config.SCREEN_WIDTH + car.width/2) {
            //ship has moved off the right side,
            //so make it wrap to the left side...
            car.posX = 0;
        }
        if(car.posY < 0 - car.height/2){
            //ship has moved off the bottom of the scene,
            //so make it wrap to the top...
            car.posY = Config.SCREEN_HEIGHT;
        }
        if(car.posY > Config.SCREEN_HEIGHT + car.height/2 )
        {
            //ship has moved off the top of the scene,
            //so make it wrap to the bottom...
            car.posY = 0;
        }
    }

};
